<?php
class post extends skeleton {
	function css($echo = true) {
		$css = ' id="post-'.$this->get_id().'" class="post"';
		
		if ($echo === true) echo $css;
		else return $css;
	}
	
	function get_author_object() {
		if (!isset($this->author_object)) {
			$this->author_object = get_single_item(array(
				'table' => USERS,
				'class' => 'user',
				'where' => '`user_id` = "'.$this->author.'"'
			));
		}
		
		return $this->author_object;
	}
	
	function get_comment_amount($word = 'Comment') {
		global $mysql;
		if (!isset($this->comment_amount)) $this->comment_amount = (int)mysql_result($mysql->query('SELECT COUNT(*) FROM `'.COMMENTS.'` WHERE `comment_post_id` = "'.$this->get_id().'" AND `comment_status` = "1"'), 0);
		
		return (!empty($word)) ? $this->comment_amount.' '.noun2plural($word, $this->comment_amount) : $this->comment_amount;
	}
	
	function get_post_id()
	{
		return $this->id;
	}
	
	function get_post_cat()
	{
		return $this->post_category;
	}
	
	function get_titletag()
	{
		return $this->post_titletag;
	}
	
	function get_metatag()
	{
		return $this->post_metatag;
	}
	
	function get_comment_status() {
		return (bool)$this->comments;
	}
	
	function get_content($format = true) {
		return ($format === true) ? autop(abbr($this->content)) : $this->content;
	}
	
	function get_custom_field($key = '') {
		if (empty($key)) return false;
		
		$array = unserialize($this->custom_fields);
		return (array_key_exists($key, (array)$array)) ? $array[$key] : false;
	}
	
	function get_date($format = 'F j, Y @ g:ia') {
		return get_date($format, $this->date, true);
	}
	
	function get_excerpt($format = true, $forced = true) {
		$excerpt = $this->excerpt;
		if ($forced === true) $excerpt = get_excerpt($this->content);
		
		return ($format === true) ? autop(abbr($excerpt)) : $excerpt;
	}
	
	function get_mini_permalink() {
		return (get_option('pretty_permalinks')) ? get_option('site_path').'id/'.$this->id.'/' : get_option('site_path').'?id='.$this->id;
	}
	
	function get_permalink($format = true, $options = array()) {
		$permalink = (get_option('pretty_permalinks')) ? get_option('site_path').'entry/'.$this->title_slug.'/' : get_option('site_path').'?entry='.$this->title_slug;
		
		if ($format === true) {
			$options = array_merge(array(
				'text' => $this->get_title(),
				'title' => $this->get_title(),
				'class' => ''
			), (array)$options);
			$options['href'] = $permalink;
			
			$permalink = make_link($options);
		}
		
		return $permalink;
	}
	
	function get_slug() {
		return $this->title_slug;
	}
	
	function get_status() {
		return (bool)$this->status;
	}
	
	function get_title() {
		return htmlspecialchars($this->title);
	}
	
	function the_author($options = array()) {
		echo $this->get_author_object()->get_permalink(true, $options);
	}
	
	function the_content($format = true) {
		echo $this->get_content($format);
	}
	
	function the_date($format = 'F j, Y @ g:ia') {
		echo $this->get_date($format);
	}
	
	function the_categories($glue = ', ', $echo = true) {
		$pretty = array();
		$explode = explode(',', $this->category);
		foreach($explode as $id) {
			$category = get_category('id', $id);
			if ($category !== false) $pretty[] = $category->get_permalink(true, true, false);
		}
		
		if ($echo === true) echo implode($glue, $pretty);
		else return implode($glue, $pretty);
	}
	
	function the_comment_link($format = true, $options = array(), $echo = true) {
		$link = $this->get_permalink(false).'#comments';
		
		if ($format === true) {
			$options = array_merge(array(
				'text' => 'View Comments',
				'title' => 'View Comments',
				'class' => ''
			), $options);
			$options['href'] = $link;
			
			$link = make_link($options);
		}
		
		if ($echo === true) echo $link;
		else return $link;
	}
	
	function the_excerpt($format = true, $forced = true) {
		echo $this->get_excerpt($format, $forced);
	}
	
	function the_permalink($format = true, $options = array()) {
		echo $this->get_permalink($format, $options);
	}
	
	function the_slug() {
		echo $this->get_slug();
	}
	
	function the_title() {
		echo $this->get_title();
	}
}
?>